package cn.robot.upload.task;

import cn.robot.upload.service.FileService;
import cn.robot.upload.service.IMinerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.File;
import java.util.List;

/**
 * @version V3.0
 * @Title: MinerTask
 * @Description: 描述
 * @author: Trident
 * @date 2020/10/4 下午9:42
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@Slf4j
@EnableScheduling
public class MinerTask {

    @Autowired
    private IMinerService minerService;
    @Autowired
    private FileService fileService;

    @Scheduled(fixedRate = 900 * 1000)
    @Async
    void uploadFile() {
        List<String> list = minerService.getList();
        list.forEach(item -> {
            try {
                int i = (int) (1 + Math.random() * (10000 - 1 + 1));
                String name = String.valueOf(i) + ".jxs";
                fileService.createFile("/file/", name);
                fileService.upload(name, item, "ddd3c2b28e1441b7b1fbc072d4897852");
                //删除文件
                File file = new File("/file/" + name);
                file.delete();
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
